using Infrastructure.Attribute;
using Infrastructure.Extensions;
using JTZY.Model.Order.Dto;
using JTZY.Model.Order;
using JTZY.Repository;
using JTZY.Service.Order.IOrderService;
using JTZY.Model.Product;

namespace JTZY.Service.Order
{
    /// <summary>
    /// 退货明细Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(IBillaftersaledetailService), ServiceLifetime = LifeTime.Transient)]
    public class BillaftersaledetailService : BaseService<Billaftersaledetail>, IBillaftersaledetailService
    {
        /// <summary>
        /// 查询退货明细列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<BillaftersaledetailDto> GetList(BillaftersaledetailQueryDto parm)
        {
            var predicate = QueryExp(parm);

            var response = Queryable()
                .Where(predicate.ToExpression())
                .ToPage<Billaftersaledetail, BillaftersaledetailDto>(parm);

            return response;
        }


        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public Billaftersaledetail GetInfo(long Id)
        {
            var response = Queryable()
                .Where(x => x.Id == Id)
                .First();

            return response;
        }

        /// <summary>
        /// 添加退货明细
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public Billaftersaledetail AddBillaftersaledetail(Billaftersaledetail model)
        {
            return Insertable(model).ExecuteReturnEntity();
        }

        /// <summary>
        /// 修改退货明细
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateBillaftersaledetail(Billaftersaledetail model)
        {
            return Update(model, true);
        }

        public List<BillaftersaledetailDto> GetDetails(QueryDetailsDto parm)
        {
            return Queryable().LeftJoin<Prodinfo>((it, prod) => it.SnCode == prod.SnCode)
                .LeftJoin<Corecmsbillaftersalesitem>((it, prod, item) => it.ProductId == item.ProductId&& it.AftersalesId==item.AftersalesId)
                .Where((it, prod, item) => it.AftersalesId == parm.aftersalesId)
                .Select((it, prod, item) => new BillaftersaledetailDto
                {
                    ImageUrl = item.ImageUrl,
                    Addon = item.Addon,
                    SnCode = prod.SnCode,
                    productDate = prod.ProdctDate,
                    UserId = prod.UserId.HasValue ? prod.UserId.Value : 0,
                }).ToList();
        }

        /// <summary>
        /// 查询导出表达式
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        private static Expressionable<Billaftersaledetail> QueryExp(BillaftersaledetailQueryDto parm)
        {
            var predicate = Expressionable.Create<Billaftersaledetail>();

            return predicate;
        }
    }
}